PowerShell History Manage Setting¶
모의해킹 및 침투 테스트 점검을 수행할 때, Terminal을 사용할 경우 History 관리
- 관리 대상 :
PowerShell
1. PowerShell Log 경로¶
명령어 입력 기록이 저장된 파일은 명령어만 저장하는 고정 포맷으로 변경할 수 없음
- PowerShell 로그의 경로를 출력하는 코드
(Get-PSReadlineOption).HistorySavePath
//C:\Users\{user}\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt
2. PowerShell의 프로필 설정¶
별도의 로그 파일로 저장하도록 설정하는 과정으로, Microsoft.PowerShell_profile.ps1 파일은 기본적으로 없기 때문에 생성 필요
$PROFILE #설정 파일 위치 탐색으로, 결과 값이 없으면 생성 필요
#결과: C:\Users\{Users}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
#파일생성
New-Item -Path $PROFILE -ItemType File -Force
notepad $PROFILE #메모장으로 생성한 파일 열기
- 명령어 실행 결과와 날짜를 파일로 저장하는 코드 추가
- 경로/파일명 :
C:\Users\{user}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
- 경로/파일명 :
#notepad $PROFILE 에 적용
Set-PSReadlineOption -AddToHistoryHandler {
param($command)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp $command" | Out-File -Append -FilePath "C:\Users\$env:USERNAME\Documents\WindowsPowerShell\ps_history_readable.txt"
return $true
}
# 명령어 + 결과 저장
$LogPath = "C:\Users\$env:USERNAME\Documents\WindowsPowerShell\logs"
if (!(Test-Path $LogPath)) { New-Item -ItemType Directory -Path $LogPath }
Start-Transcript -Path "$LogPath\session_$(Get-Date -Format 'yyyy_MM_dd').txt" -Append
위 코드를 적용하고 PowerShell을 실행하면 기록 관련 메시지가 출력되고 2개의 파일에 데이터가 저장된다.
\Documents\WindowsPowerShell\ps_history_readable.txt: 입력 명령어+시간\Documents\WindowsPowerShell\logs: 명령어 입력 및 출력 결과를 시간별 저장
#저장 및 적용 (안될 경우, Truble Shotting [1] 참고)
#실행은 아무 위치에서 가능
. $PROFILE
#실행결과
기록이 시작되었습니다. 출력 파일은 C:\Users\{user}\Documents\WindowsPowerShell\logs\session_YYYY_MM_DD.txt입니다.
3. PowerShell 명령어 기록 설정 코드¶
PowerShell 권한 확인 및 전체 과정 자동화 스크립트는 아래와 같이 구성할 수 있다.
.bat으로 저장해서 실행하면 코드 기반으로 자동 설정 진행
@echo off
:: 한글 처리
chcp 65001 > nul
echo PowerShell History 설정을 시작합니다...
:: 1. ExecutionPolicy 설정
echo [*] ExecutionPolicy 설정 시작...
powershell -Command "$policy = Get-ExecutionPolicy -Scope CurrentUser; Write-Host '[*] 현재 ExecutionPolicy:' $policy; if ($policy -ne 'RemoteSigned') { Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force; Write-Host '[+] ExecutionPolicy 변경완료' } else { Write-Host '[*] ExecutionPolicy 이미 설정됨' }"
:: 2. Profile 폴더 생성
if not exist "C:\Users\%USERNAME%\Documents\WindowsPowerShell\" (
echo [*] Profile 폴더 생성 중...
mkdir "C:\Users\%USERNAME%\Documents\WindowsPowerShell"
echo [+] Profile 폴더 생성 완료
)
if not exist "C:\Users\%USERNAME%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1" (
echo [*] Profile 파일 생성 중...
type nul > "C:\Users\%USERNAME%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
echo [+] Profile 파일 생성 완료
) else (
echo [*] Profile 파일 이미 존재함
)
:: 3. Profile 파일 생성 및 내용 작성
(
echo Set-PSReadLineOption -AddToHistoryHandler {
echo param^($command^)
echo $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
echo "$timestamp $command" ^| Out-File -Append -FilePath "C:\Users\$env:USERNAME\Documents\WindowsPowerShell\ps_history_readable.txt"
echo return $true
echo }
echo.
echo # 명령어 + 결과 저장
echo $LogPath = "C:\Users\$env:USERNAME\Documents\WindowsPowerShell\logs"
echo if ^(!^(Test-Path $LogPath^)^) { New-Item -ItemType Directory -Path $LogPath }
echo Start-Transcript -Path "$LogPath\session_$(Get-Date -Format 'yyyy_MM_dd').txt" -Append
) > "C:\Users\%USERNAME%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
echo [+] Profile 파일 작성 완료
echo [+] Any Press Key Exit...
pause > nul
- Additional Options
export HISTSIZE={size} #History에 저장할 크기 별도 지정(기본 1000줄)
export HISTFILESIZE={size}
export HISTCONTROL=ignoredups:ignorespace #중복 명령어 저장 안함 설정
Trouble Shooting¶
.$PROFILE을 적용할 때, 다음과 같은 오류가 발생하는 경우 - PowerShell 스크립트 실행 정책에 의해서Restricted로 설정되어 오류가 발생하기 때문에, 로컬에서 만든 스크립트는 실행을 허용하도록 설정 변경Restricted>RemoteSigned(인터넷에서 받은 것만 서명하고 실행)PS> . $PROFILE + . $PROFILE + ~~~~~~~~ + CategoryInfo : 보안 오류: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess- 설정 변경 명령어
#정책을 변경할 때, 현재 사용자의 권한만 변경하도록 설정 Set-ExecutionPolicy RemoteSigned -Scope CurrentUser #변경 후, 권한 확인 Get-ExecutionPolicy